import 'package:shared_preferences/shared_preferences.dart';

class Store {
  static Store? _singleton;
  static SharedPreferences? _preferences;

  static Future<Store?> getInstance() async {
    // ignore: unnecessary_null_comparison
    if (_singleton == null) {
      _singleton = Store._internal();
      await _singleton?._init();
    }
    return _singleton;
  }

  Store._internal();

  Future _init() async {
    _preferences = await SharedPreferences.getInstance();
  }

  dynamic get(String key) {
    return _preferences?.get(key);
  }

  Future<bool>? set(String key, dynamic value) {
    if (value is bool) {
      return _preferences?.setBool(key, value);
    } else if (value is int) {
      return _preferences!.setInt(key, value);
    } else if (value is double) {
      return _preferences?.setDouble(key, value);
    } else if (value is String) {
      return _preferences?.setString(key, value);
    } else if (value is List<String>) {
      return _preferences?.setStringList(key, value);
    } else {
      return Future.value(false);
    }
  }

  Future<bool>? remove(String key) {
    return _preferences?.remove(key);
  }

  Future<bool>? clear() {
    return _preferences?.clear();
  }
}
